<?php

namespace Unit\App;

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Illuminate\Http\UploadedFile;
use org\bovigo\vfs\vfsStream;

class TestCase extends BaseTestCase
{
    use CreatesApplication;

    /**
     * create an uploaded file from file on file system
     *
     * @param string      $path path of file to be uploaded (MUST exist)
     * @param string|null $name file name. if not specified, basename of $path will be applied
     * @param string|null $mime client mime/content-type of file
     *
     * @return UploadedFile
     */
    protected final function makeUploadFile($path, $name = null, $mime = null)
    {
        if (empty($name)) {
            $name = pathinfo($path, PATHINFO_BASENAME);
        }

        return new UploadedFile($path, $name, $mime, filesize($path), null, true);
    }

    /**
     * mock files on file system
     *
     * @param string     $root  root directory
     * @param array|null $files directory structure to add under root directory, refer to vfsStream for more
     *
     * @see \org\bovigo\vfs\vfsStream::create()
     */
    protected final function mockFiles($root, array $files = null)
    {
        vfsStream::setup($root, null, $files ?: []);
    }

    /**
     * get the path of a mocked file
     *
     * @param string $path relative path (without root directory and scheme)
     *
     * @return string        file path on mocked file system
     */
    protected final function getMockedFilePath($path)
    {
        return vfsStream::url($path);
    }

    /**
     * normalize file path
     *
     * @param $path
     *
     * @return mixed
     */
    protected final function normalizeFilePath($path)
    {
        if (DIRECTORY_SEPARATOR == '\\') {
            return str_replace('/', DIRECTORY_SEPARATOR, $path);
        }

        return $path;
    }

}
